#include <bits/stdc++.h>
using namespace std;
int n,m,k,t,a[100002];
string s;
int K(int n)
{
	int i=1;
	for(;i<n;i<<=1);
	return i;
}
int main()
{
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%d",a+i);
	}
	int k=K(n);
	while(k--)
	{
		cin>>s;
	}
	scanf("%d",&t);
	while(t--)
	{
		int x1,x2,x3,x4;
		scanf("%d%d%d%d",&x1,&x2,&x3,&x4);
		srand(x1+x2+x3+x4);
		printf("%d\n",rand());
	}
	return 0;
} 
